package two.chapter_2.c2_1;

import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import two.chapter_1.c1_4.Stopwatch;

import javax.swing.plaf.nimbus.AbstractRegionPainter;

public class SortCompare {
    public static final String SORT_INSERTION="INSERTION";
    public static final String SORT_SELECTION="SELECTION";
    public static final String SORT_SHELL="SHELL";
    public static double time(String alg, Double[] a) {
        Stopwatch stopwatch = new Stopwatch();
        Sortable<Double> sort=null;
        if (alg.equals(SORT_INSERTION)) sort = new InsertionSort<>();
        if (alg.equals(SORT_SELECTION)) sort = new SelectionSort<>();
        if (alg.equals(SORT_SHELL)) sort = new ShellSort<>();

        sort.sort(a);
        return stopwatch.elapseTime();

    }

    public static double timeRandomInput(String alg,int N,int T){
        double total=0.0;
        Double[] a=new Double[N];
        for (int t=0;t<T;t++){
            for(int i=0;i<N;i++){
                a[i]=1.0;//StdRandom.uniform();
            }
            total+=time(alg,a);
        }
        return total;
    }

    public static void main(String[] args){
        final String arg1=SORT_SELECTION;
        final String arg2=SORT_INSERTION;
        final int N=1000;
        final int T=100;

        new Thread(new Runnable() {
            @Override
            public void run() {
                double t1=timeRandomInput(arg1,N,T);
                StdOut.printf("%s %.6f",arg1,t1);
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                double t2=timeRandomInput(arg2,N,T);
                StdOut.printf(" %s %.6f",arg2,t2);
            }
        }).start();

       /*

        StdOut.printf("For %d random Doubles\n %s is",N,arg1);
        StdOut.printf(" %.1f times faster than %s\n",t2/t1,arg2);*/
    }
}
